withTransaction
Purpose
Allows programmatic transactions using Spring's Transaction Abstraction and a block.Examples
Account.withTransaction { status ->
def source = Account.get(params.from)
def dest = Account.get(params.to) def amount = params.amount.toInteger()
if(source.active) {
source.balance -= amount
if(dest.active) {
dest.amount += amount
}
else {
status.setRollbackOnly()
}
}
}
Description
The withTransaction
method uses a block or closure that gets passed Spring TransactionStatus object. The TransactionStatus
object can be used to programmatically control rollback of the transaction.Refer to the user guide section of Programmatic Transactions for more information.